package com.bdqn.test1.service;

import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import java.io.File;

@Service
public class SendMailServiceImpl2 implements SendMailService{


    @Autowired
    private JavaMailSender javaMailSender;

    //发送人
    private String from = "2053932004@qq.com";
    //接收人
//    private String to = "2053932004@qq.com";
    private String to = "3482992132@qq.com";
//    private String to = "1739402313@qq.com";
    //标题
    private String subject = "测试邮件";
    //正文
    private String context = "<h1><a href='https://www.yuque.com/arvingao/spring-boot/ll7nuci7ls9cd918#Q58gf'>点开有惊喜</a></h1>" +
            "<img src='https://img1.baidu.com/it/u=3772072020,2024906464&fm=253&fmt=auto&app=120&f=JPEG?w=1422&h=800' />";
    @Override
    public void sendMail() {
        //简单邮件
//        SimpleMailMessage message = new SimpleMailMessage();
//        message.setFrom(from + "(小甜甜)");
//        message.setTo(to);
//        message.setSubject(subject);
//        message.setText(context);

        //复杂邮件
        try {
            MimeMessage message = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message,true);
            helper.setFrom(from + "(小甜甜)");
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(context,true);

            //添加组件
            File f1 = new File("E:\\JavaS3\\Spring Boot\\Mail\\test1\\target\\test1-0.0.1-SNAPSHOT.jar");
            File f2 = new File("E:\\JavaS3\\Spring Boot\\Mail\\test1\\src\\main\\resources\\微信图片2_20230603134531.jpg");
            helper.addAttachment(f1.getName(), f1);
            helper.addAttachment("哇哦.jpg",f2);
            javaMailSender.send(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
